package com.rla.controller;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.rla.common.annotation.RepeatSubmit;
import com.rla.common.annotation.Log;
import com.rla.common.core.controller.BaseController;
import com.rla.common.core.domain.PageQuery;
import com.rla.common.core.domain.R;
import com.rla.common.utils.validate.AddGroup;
import com.rla.common.utils.validate.EditGroup;
import com.rla.common.enums.BusinessType;
import com.rla.common.utils.poi.ExcelUtil;
import com.rla.domain.vo.BaseDrugSpeciVo;
import com.rla.domain.bo.BaseDrugSpeciBo;
import com.rla.service.IBaseDrugSpeciService;
import com.rla.common.core.page.TableDataInfo;

/**
 * 药品-规格对照
 *
 * @author rla
 * @date 2025-08-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/drug/speci")
public class BaseDrugSpeciController extends BaseController {

    private final IBaseDrugSpeciService iBaseDrugSpeciService;

    /**
     * 查询药品-规格对照列表
     */
    @GetMapping("/list")
    public TableDataInfo<BaseDrugSpeciVo> list(BaseDrugSpeciBo bo, PageQuery pageQuery) {
        return iBaseDrugSpeciService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出药品-规格对照列表
     */
    @Log(title = "药品-规格对照", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BaseDrugSpeciBo bo, HttpServletResponse response) {
        List<BaseDrugSpeciVo> list = iBaseDrugSpeciService.queryList(bo);
        ExcelUtil.exportExcel(list, "药品-规格对照", BaseDrugSpeciVo.class, response);
    }

    /**
     * 获取药品-规格对照详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<BaseDrugSpeciVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iBaseDrugSpeciService.queryById(id));
    }

    /**
     * 新增药品-规格对照
     */
    @Log(title = "药品-规格对照", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BaseDrugSpeciBo bo) {
        return toAjax(iBaseDrugSpeciService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改药品-规格对照
     */
    @Log(title = "药品-规格对照", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BaseDrugSpeciBo bo) {
        return toAjax(iBaseDrugSpeciService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除药品-规格对照
     *
     * @param ids 主键串
     */
    @Log(title = "药品-规格对照", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iBaseDrugSpeciService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
